﻿using Apple.BLL.Dto.RecipeDto;
using Apple.BLL.Manager;
using Apple.Helper;
using HZY.Framework.DependencyInjection;
using IoTClient.Enums;
using Mapster;
using Masuit.Tools.Reflection;
using MiniExcelLibs;
using SqlSugar.Extensions;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LogLevel = Microsoft.Extensions.Logging.LogLevel;

namespace Apple.SprayingProcessSystem
{
    public partial class PageRecipeManage : UIPage, ISingletonSelfDependency
    {
        private readonly RecipeManager _recipeManager;

        public PageRecipeManage(RecipeManager recipeManager)
        {
            InitializeComponent();
            this._recipeManager = recipeManager;
            this.dgv_Recipe.AutoGenerateColumns = false; // 禁用自动生成列
            this.Load += PageRecipeManage_Load;
        }

        private async void PageRecipeManage_Load(object? sender, EventArgs e)
        {
            await LoadRecipe();
        }

        private async Task LoadRecipe()
        {
            var res = await _recipeManager.GetRecipeListAsync();
            if (res.Status == SystemEnums.Result.Success)
            {
                //this.dgv_Recipe.DataSource = null;
                this.dgv_Recipe.DataSource = res.Data;
            }
            else
            {
                LogExtension.ShowMessage(res.Msg, LogLevel.Error);
            }
        }

        /// <summary>
        /// 添加配方
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void btn_AddRecipe_Click(object sender, EventArgs e)
        {
            if (this.txt_ProductType.Text.IsNullOrEmpty())
            {
                UIMessageTip.ShowWarning("请输入产品类型");
                return;
            }

            AddRecipeDto addDto = new AddRecipeDto()
            {
                产品类型 = this.txt_ProductType.Text.Trim()
            };

            foreach (var item in this.uiTitlePanel1.Controls)
            {
                if (item is UserSetValue usv)
                {
                    // 反射赋值
                    addDto?.GetType()?.GetProperty(usv.VariableName)?.SetValue(addDto, usv.VarValue);
                }
            }

            BaseResult res = await _recipeManager.AddRecipeAsync(addDto);

            if (res.Status == SystemEnums.Result.Success)
            {
                UIMessageTip.ShowOk("添加配方成功");
                await LoadRecipe(); // 刷新配方列表
            }
            else
            {
                LogExtension.ShowMessage(res.Msg, LogLevel.Error);
            }
        }

        /// <summary>
        /// 删除配方
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void btn_DelRecipe_Click(object sender, EventArgs e)
        {
            if (this.dgv_Recipe.CurrentRow == null)
            {
                UIMessageTip.ShowWarning("请选择一行");
                return;
            }

            var id = this.dgv_Recipe.CurrentRow.Cells["Id"].Value.ToString().ToInt();

            var res = await _recipeManager.DeleteRecipeAsync(new DelRecipeDto() { Id = id });

            if (res.Status == SystemEnums.Result.Success)
            {
                UIMessageTip.ShowOk("删除配方成功");
                await LoadRecipe();
            }
            else
            {
                LogExtension.ShowMessage(res.Msg, LogLevel.Error);
                UIMessageTip.ShowError(res.Msg);
            }
        }

        /// <summary>
        /// dgv_Recipe 选中行改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="index"></param>
        private async void dgv_Recipe_SelectIndexChange(object sender, int index)
        {
            int Id = this.dgv_Recipe.Rows[index].Cells["Id"].Value.ToString().ToInt();

            BaseResult<QueryRecipeResultDto> res = await _recipeManager.GetRecipeAsyncById(new GetRecipeByIdDto() { Id = Id });

            if (res.Status == SystemEnums.Result.Success)
            {
                // 反射给控件赋值
                foreach (var item in uiTitlePanel1.Controls)
                {
                    if (item is UserSetValue usv)
                    {
                        var value = res.Data[0]?.GetType()?.GetProperty(usv.VariableName)?.GetValue(res.Data[0]);
                        usv.VarValue = value?.ToString();
                    }
                }

                this.txt_ProductType.Text = res.Data[0].产品类型;
            }
        }

        /// <summary>
        /// 修改配方
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void btn_UpdateRecipe_Click(object sender, EventArgs e)
        {
            if (this.dgv_Recipe.CurrentRow == null)
            {
                UIMessageTip.ShowWarning("请选择一行");
                return;
            }

            UpdateRecipeDto updateDto = new UpdateRecipeDto();
            var id = this.dgv_Recipe.CurrentRow.Cells["Id"].Value.ToString().ToInt();
            updateDto.Id = id;
            updateDto.产品类型 = this.txt_ProductType.Text;

            foreach (var item in uiTitlePanel1.Controls)
            {
                if (item is UserSetValue usv)
                {
                    updateDto?.GetType()?.GetProperty(usv.VariableName)?.SetValue(updateDto, usv.VarValue);
                }
            }
            var res = await _recipeManager.UpdateRecipeAsync(updateDto);
            if (res.Status == SystemEnums.Result.Success)
            {
                UIMessageTip.ShowOk("修改配方成功");
                await LoadRecipe();
            }
            else
            {
                UIMessageTip.ShowError(res.Msg);
                LogExtension.ShowMessage(res.Msg, LogLevel.Error);
            }
        }

        /// <summary>
        /// 查询配方
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void btn_QueryRecipe_Click(object sender, EventArgs e)
        {
            await LoadRecipe();
        }

        /// <summary>
        /// 导出配方
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void btn_ExportRecipe_Click(object sender, EventArgs e)
        {
            using (SaveFileDialog saveFileDialog = new SaveFileDialog())
            {
                saveFileDialog.Filter = "Excel files(*.xlsx)|*.xlsx";
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        List<QueryRecipeResultDto> rows = this.dgv_Recipe.DataSource as List<QueryRecipeResultDto>;
                        var filePath = saveFileDialog.FileName;
                        await MiniExcel.SaveAsAsync(filePath, rows);
                    }
                    catch (Exception ex)
                    {
                        UIMessageBox.ShowError("导出失败");
                        LogExtension.ShowMessage(ex.Message, LogLevel.Error);
                    }
                }

            }
        }

        /// <summary>
        /// 导入配方
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void btn_ImportRecipe_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                try
                {
                    openFileDialog.Filter = "Excel files(*.xlsx)|*.xlsx";
                    if (openFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        var filePath = openFileDialog.FileName;

                        var rows = MiniExcel.Query<QueryRecipeResultDto>(filePath).ToList();

                        //查询数据库的配方，如果导入的配方名称数据库没有，那就添加，如果有 就更新
                        var res = await _recipeManager.GetRecipeListAsync();
                        if (res.Status == SystemEnums.Result.Success)
                        {
                            var dbRows = res.Data;
                            foreach (var row in rows)
                            {
                                var dbRow = dbRows.FirstOrDefault(a => a.产品类型 == row.产品类型);
                                // 更新
                                if (dbRow != null)
                                {
                                    var updateDto = row.Adapt<UpdateRecipeDto>();
                                    updateDto.Id = dbRow.Id; // 设置ID
                                    var res1 = await _recipeManager.UpdateRecipeAsync(updateDto);
                                    if (res1.Status != SystemEnums.Result.Success)
                                    {
                                        UIMessageTip.ShowError("导入失败:" + res1.Msg);
                                        return;
                                    }
                                }
                                else
                                {
                                    // 添加
                                    var addDto = row.Adapt<AddRecipeDto>();
                                    var res2 = await _recipeManager.AddRecipeAsync(addDto);
                                    if (res2.Status != SystemEnums.Result.Success)
                                    {
                                        UIMessageTip.ShowError("导入失败:" + res2.Msg);
                                        return;
                                    }
                                }
                            }

                            UIMessageTip.ShowOk("导入成功");
                            await LoadRecipe(); // 刷新配方列表
                        }
                        else
                        {
                            UIMessageTip.ShowError("导入失败:" + res.Msg);
                        }

                    }
                }
                catch (Exception ex)
                {
                    UIMessageTip.ShowError("导入失败:" + ex.Message);
                }

            }
        }

        /// <summary>
        /// 下载配方
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_DownloadRecipe_Click(object sender, EventArgs e)
        {
            if (!Globals.SiemensClient.Connected)
            {
                UIMessageTip.ShowWarning("请先连接西门子PLC");
                return;
            }

            bool res = UIMessageBox.ShowAsk("是否要下载到西门子PLC");
            if (res)
            {
                foreach (Control item in uiTitlePanel1.Controls)
                {
                    if (item is UserSetValue usv)
                    {
                        // 根据类型设定写入PLC的数据类型
                        DataTypeEnum dataType = DataTypeEnum.Int32;

                        if (usv.DataType==UITextBox.UIEditType.Double)
                        {
                            dataType = DataTypeEnum.Float;
                        }

                        if (!Globals.PlcWrite(usv.VariableName, usv.VarValue, dataType))
                        {
                            new UIMessageForm().ShowErrorDialog($"{usv.VariableName}下载失败");
                            return;
                        }
                    }
                }

                UIMessageTip.ShowOk("下载成功!");
            }
        }

        #region 窗体切换卡顿处理
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams paras = base.CreateParams;
                paras.ExStyle |= 0x02000000;
                return paras;
            }
        }
        #endregion
    }
}
